package cn.openea.perpartner.evaluation.web.controller;

import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.evaluation.web.entity.EvaluationSignup;
import cn.openea.perpartner.evaluation.web.entity.vo.SignupDataVo;
import cn.openea.perpartner.evaluation.web.service.IEvaluationSignupService;
import cn.openea.perpartner.meily.aspect.annotation.AccessLimit;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author rains
 * @since 2023-07-06 15:49
 */
@Api(tags = "报名参与品牌推广")
@RestController
@RequestMapping("/evaluation/signup")
public class EvaluationSignupController extends BaseController {

    @Resource
    private IEvaluationSignupService evaluationSignupService;

    @ApiOperation("获取报名详情")
    @GetMapping("/info/{id}")
    public AjaxResult info(@PathVariable String id) {
        if (StringUtils.isBlank(id)) {
            return AjaxResult.error("数据异常，id为空");
        }
        return AjaxResult.success("获取报名详情！", this.evaluationSignupService.getById(id));
    }

    @ApiOperation(value = "校验是否可以报名")
    @PostMapping("/verify")
    public AjaxResult verifyRegistration(@RequestBody @Valid EvaluationSignup evaluationSignup) {
        JSONObject object = this.evaluationSignupService.verifyRegistration(evaluationSignup);
        if (object.getInteger("code") != 200) {
            AjaxResult ajaxResult = AjaxResult.error(object.getInteger("code"), object.getString("msg"));
            ajaxResult.put("url", object.getString("url"));
            return ajaxResult;
        }
        return AjaxResult.success("可以报名！");
    }

    @ApiOperation(value = "报名参与")
    @PostMapping("/join")
    @AccessLimit(lockTime = 10)
    public AjaxResult join(@RequestBody EvaluationSignup evaluationSignup) {
        return AjaxResult.success("报名成功！", evaluationSignupService.join(evaluationSignup));
    }

    @ApiOperation(value = "提交成果")
    @PostMapping("/submit/result")
    @AccessLimit(lockTime = 10)
    public AjaxResult submitResult(@RequestBody EvaluationSignup evaluationSignup) {
        if(!evaluationSignupService.submitResult(evaluationSignup)){
            return AjaxResult.error("提交失败！");
        }
        return AjaxResult.success("提交成功！");
    }

    @ApiOperation(value = "验收通过")
    @PostMapping("/accept/pass")
    @AccessLimit(lockTime = 10)
    public AjaxResult pass(@RequestBody EvaluationSignup evaluationSignup) {
        if(!evaluationSignupService.pass(evaluationSignup)){
            return AjaxResult.error("验收失败！");
        }
        return AjaxResult.success("验收成功！");
    }

    @ApiOperation(value = "验收不通过(退回/回收)")
    @PostMapping("/accept/disagree")
    @AccessLimit(lockTime = 10)
    public AjaxResult disagree(@RequestBody EvaluationSignup evaluationSignup) {
        if(!evaluationSignupService.disagree(evaluationSignup)){
            return AjaxResult.error("退回失败！");
        }
        return AjaxResult.success("退回成功！");
    }

    @ApiOperation(value = "评级")
    @PostMapping("/rating")
    @AccessLimit(lockTime = 10)
    public AjaxResult rating(@RequestBody SignupDataVo vo) {
        if(!evaluationSignupService.rating(vo)){
            return AjaxResult.error("评级失败！");
        }
        return AjaxResult.success("评级成功！");
    }

    @ApiOperation(value = "回收名额")
    @PostMapping("/recyclingQuota")
    public AjaxResult recyclingQuota(@RequestBody SignupDataVo vo) {
        return evaluationSignupService.recyclingQuota(vo);
    }

    @ApiOperation(value = "获取未提交成果的报名记录")
    @GetMapping("/getNotSubmit/{partnerId}")
    public AjaxResult getNotSubmit(@PathVariable @Valid String partnerId) {
        return AjaxResult.success("获取未提交成果的报名记录成功！", evaluationSignupService.getNotSubmit(partnerId));
    }

    @ApiOperation(value = "获取排行榜记录")
    @PostMapping("/ranking/list")
    public AjaxResult getRankingList(@RequestBody SignupDataVo vo) {
        startPage();
        List<EvaluationSignup> list = evaluationSignupService.getRankingList(vo);
        TableDataInfo tableDataInfo = getDataTable(list);
        AjaxResult ajaxResult = AjaxResult.success("查询成功！", tableDataInfo.getRows());
        ajaxResult.put("total", tableDataInfo.getTotal());
        ajaxResult.put("title", evaluationSignupService.getRatingListTitle(vo.getMainId()));
        return ajaxResult;
    }

    @ApiOperation(value = "获取已通过验收的报名记录--不分页")
    @PostMapping("/pass/list")
    public AjaxResult getPassList(@RequestBody SignupDataVo vo) {
        return AjaxResult.success("获取已通过验收的报名记录成功！", evaluationSignupService.getPassList(vo));
    }

    @ApiOperation(value = "获取报名记录")
    @PostMapping("/page/list")
    public TableDataInfo selectList(@RequestBody SignupDataVo vo){
        startPage();
        List<EvaluationSignup> list = evaluationSignupService.selectList(vo);
        return getDataTable(list);
    }

    @ApiOperation("补全迁移的历史数据")
    @PostMapping("/deal/historical")
    public AjaxResult dealHistoricalData(@RequestBody JSONObject object){
        if("1".equals(object.getString("runningFlag"))){
            if(!this.evaluationSignupService.dealHistoricalData()){
                return AjaxResult.error("补全迁移的历史数据失败！");
            }
        } else {
            return AjaxResult.error("执行标志的参数传输错误，不执行！");
        }
        return AjaxResult.success("补全迁移的历史数据成功！");
    }

}
